<?php

class Application_Model_Narocilo
{
    protected $_idnarocilo;
    protected $_kosarica_sifra_kosarice;
    protected $_stranka_emso;
    protected $_status;

    const STATUS_NOV = 0;
    const STATUS_ODOBRENO = 1;
    const STATUS_ZAVRNJENO = 2;
    const STATUS_STORNIRANO = 3;
    
     public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid product property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid product property');
        }
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function getIdNarocilo() {
        return $this->_idnarocilo;
    }

       public function getKosarica_sifra_kosarice()
    {
        return $this->_kosarica_sifra_kosarice;
    }

    public function getStranka_emso(){
        
        return $this->_stranka_emso;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($status) {
        $this->_status = $status;
        return $this;
    }
    
    public function setKosarica_sifra_kosarice($kosarica_sifra_kosarice)
    {
        $this->_kosarica_sifra_kosarice = $kosarica_sifra_kosarice;
        return $this;
    }

        public function setIdnarocilo($idnarocilo)
    {
        $this->_idnarocilo = $idnarocilo;
        return $this;
    }
    
    public function setStranka_emso($stranka_emso)
    {
        $this->_stranka_emso = $stranka_emso;
        return $this;
    }

    public function posodobiStatus($koda_statusa) {
        $mapper = new Application_Model_NarociloMapper();
        $mapper->update($this->_idnarocilo, 'status', $koda_statusa);
        return $this;
    }

    public function oznakaStatusa($koda_status) {
        switch ($koda_status) {
            case self::STATUS_NOV:
                return "Novo";
                break;
            case self::STATUS_ODOBRENO:
                return "Odobreno";
                break;
            case self::STATUS_STORNIRANO:
                return "Stornirano";
                break;
            case self::STATUS_ZAVRNJENO:
                return "Zavrnjeno";
                break;
            default:
                return "Neznan status: Napaka!!";
                break;
        }
    }

    public function loadById($id) {
        $narociloMapper = new Application_Model_NarociloMapper();
        $narociloRow = $narociloMapper->fetchRow($narociloMapper->getDbTable()->getAdapter()->quoteInto('idnarocilo = ?', $id));

        if (isset($narociloRow['idnarocilo']) && $narociloRow['idnarocilo']) {
            $this->setIdnarocilo($id);
            $this->setStranka_emso($narociloRow['stranka_emso']);
            $this->setKosarica_sifra_kosarice($narociloRow['kosarica_sifra_kosarice']);
            $this->setStatus($narociloRow['status']);
            return $this;
        }

        return null;
    }

    public function getUpdateUrl($baseUrl) {
        return $baseUrl."/prodaja/posodobi-narocilo/?id=".$this->getIdNarocilo();
    }

    public function getViewUrl($baseUrl) {
        return $baseUrl."/prodaja/view-narocilo/?id=".$this->getIdNarocilo();
    }
    
    public function getViewUrlNarocilo($baseUrl) {
        return $baseUrl."/seznam/view-narocilo/?id=".$this->getIdNarocilo();
    }

    public function rollbackZaloga() {
        $elementKosariceMapper = new Application_Model_ElementKosariceMapper();

        $where = $elementKosariceMapper->getDbTable()->getAdapter()
            ->quoteInto('kosarica_sifra_kosarice = ?', $this->getKosarica_sifra_kosarice());

        $elementi = $elementKosariceMapper->fetchAll($where);

        $produktMapper = new Application_Model_IzdelkiMapper();

        foreach ($elementi as $element) {
            $produktId = $element['produkt_idprodukt'];
            $produkt = new Application_Model_Izdelki();
            $produktMapper->find($produktId, $produkt);

            $produktMapper->update($produktId, "zaloga", $produkt->getZaloga()+$element['kolicina']);
        }
    }
    
}

